import React, {Component} from 'react';
import {connect} from 'react-redux';

import * as actionCreators from '../../actions/MemberAction';
import reg from "../../utils/regexp";
import ToastManager from "../../components/AroToast";
import view from './view';

const getMailCodeParams = {
  check_code_token: '1',
  check_code: '1',
  m_name: '',
  from: 'forgot'
};

const forgetParams = {
  m_name: '',
  m_pwd: '',
  m_repwd: '',
  sms_code: ''
};

class Forget extends Component {

  static navigationOptions = {
    header: null,
  };

  render() {
    return view(this, getMailCodeParams, forgetParams);
  }

  componentDidMount() {
    this.initialFormState()
  }

  initialFormState() {
    forgetParams.m_name = '';
    forgetParams.m_pwd = '';
    forgetParams.m_repwd = '';
    forgetParams.sms_code = '';
    this.props.forgetButtonState();
  }

  sendCodeHandle(shouldStartCountting, data) {
    let isPhoneAccount = reg.regPhone.test(data);
    let isMailAccount = reg.regMail.test(data);
    let isAccountEmpty = reg.regEmpty.test(data);

    if (isAccountEmpty) {
      ToastManager.show('账号不能为空', 2000);
      shouldStartCountting(false);
      return
    }

    if (!isPhoneAccount && !isMailAccount) {
      ToastManager.show('账号格式不正确', 2000);
      shouldStartCountting(false);
      return
    }

    getMailCodeParams.m_name = forgetParams.m_name;
    this.props.getMailCode(getMailCodeParams, shouldStartCountting);
  }

}

const mapStateToProps = (state) => {
  return {
    forgetButtonDisabled: state.member.forgetButtonDisabled
  }
};

const mapDispatchToProps = (dispatch) => {
  return {
    getMailCode(data, shouldStartCountting) {
      dispatch(actionCreators.getMailCode(data, shouldStartCountting))
    },
    memberForget(data) {
      dispatch(actionCreators.memberForget(data))
    },
    forgetButtonState() {
      let isForgetParamsEmpty = reg.regEmpty.test(forgetParams.m_name) || reg.regEmpty.test(forgetParams.sms_code)
        || reg.regEmpty.test(forgetParams.m_pwd) || reg.regEmpty.test(forgetParams.m_repwd);
      if (isForgetParamsEmpty) {
        dispatch(actionCreators.forgetButtonState(true))
      } else {
        dispatch(actionCreators.forgetButtonState(false))
      }
    }
  }
};

export default connect(mapStateToProps, mapDispatchToProps)(Forget)